from pyecharts import options as opts
from pyecharts.charts import Graph

# 知识节点配置
nodes = [
    {"name": "经济增长与福利分析", "symbolSize": 50},
    # 第一部分
    {"name": "全要素生产率(TFP)", "symbolSize": 40},
    {"name": "TFP增速变化", "symbolSize": 35},
    {"name": "影响因素", "symbolSize": 35},
    # 第二部分
    {"name": "劳动生产率", "symbolSize": 40},
    # 第三部分
    {"name": "收入分配结构", "symbolSize": 40},
    {"name": "分配问题", "symbolSize": 35},
    # 政策部分
    {"name": "政策启示", "symbolSize": 40},
    {"name": "具体措施", "symbolSize": 35},
    
    # 子节点
    {"name": "后发优势减弱", "symbolSize": 25},
    {"name": "经济服务化", "symbolSize": 25},
    {"name": "资源配置效率", "symbolSize": 25},
    {"name": "疫情疤痕效应", "symbolSize": 25},
    {"name": "城乡差距", "symbolSize": 25},
    {"name": "行业差距", "symbolSize": 25},
    {"name": "地区差距", "symbolSize": 25},
    {"name": "企业部门收入偏高", "symbolSize": 25},
    {"name": "调节政策不足", "symbolSize": 25}
]

# 关系连接配置
links = [
    # 总体结构
    {"source": "经济增长与福利分析", "target": "全要素生产率(TFP)"},
    {"source": "经济增长与福利分析", "target": "劳动生产率"},
    {"source": "经济增长与福利分析", "target": "收入分配结构"},
    {"source": "经济增长与福利分析", "target": "政策启示"},
    
    # TFP部分
    {"source": "全要素生产率(TFP)", "target": "TFP增速变化"},
    {"source": "全要素生产率(TFP)", "target": "影响因素"},
    {"source": "影响因素", "target": "后发优势减弱"},
    {"source": "影响因素", "target": "经济服务化"},
    {"source": "影响因素", "target": "资源配置效率"},
    {"source": "影响因素", "target": "疫情疤痕效应"},
    
    # 收入分配
    {"source": "收入分配结构", "target": "分配问题"},
    {"source": "分配问题", "target": "城乡差距"},
    {"source": "分配问题", "target": "行业差距"},
    {"source": "分配问题", "target": "地区差距"},
    {"source": "分配问题", "target": "企业部门收入偏高"},
    {"source": "分配问题", "target": "调节政策不足"},
    
    # 政策部分
    {"source": "政策启示", "target": "具体措施"},
    {"source": "具体措施", "target": "城乡差距"},
    {"source": "具体措施", "target": "企业部门收入偏高"},
    {"source": "具体措施", "target": "调节政策不足"}
]

def create_knowledge_graph():
    c = (
        Graph(init_opts=opts.InitOpts(width="1600px", height="800px"))
        .add(
            series_name="",
            nodes=nodes,
            links=links,
            layout="force",
            repulsion=500,
            linestyle_opts=opts.LineStyleOpts(width=2, curve=0.2),
            label_opts=opts.LabelOpts(
                position="right",
                formatter="{b}",
                font_size=14
            ),
            tooltip_opts=opts.TooltipOpts(
                formatter="{b}: {c}"
            )
        )
        .set_global_opts(
            title_opts=opts.TitleOpts(
                title="经济增长与国民福利关系知识图谱",
                subtitle="基于定量分析的政策研究",
                title_textstyle_opts=opts.TextStyleOpts(font_size=24),
                pos_left="center"
            )
        )
    )
    return c

# 生成HTML文件
chart = create_knowledge_graph()
chart.render("贾彦东.html")